//
//  WaveView.swift
//  HSMainViewControllerHandler
//
//  Created by haisheng huang on 2016/11/11.
//  Copyright © 2016年 haisheng huang. All rights reserved.
//

import Foundation
import UIKit
import CoreFoundation


@available(iOS 8.0, *)
class WaveView: UIView {
    
    var angularSpeed: CGFloat = 0.0
    var waveSpeed: CGFloat = 0.0
    var waveTime: TimeInterval = 0.0
    var waveColor: UIColor? = UIColor.white
    fileprivate var offsetX: CGFloat = 0.0
    fileprivate var waveDisplayLink: CADisplayLink?
    fileprivate var waveShapeLayer: CAShapeLayer?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.basicSetup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    class func addToView(_ view: UIView, frame: CGRect) -> WaveView {
        
        let wv: WaveView = WaveView.init(frame: frame)
        view.addSubview(wv)
        return wv
        
    }
    
    func basicSetup() -> Void {
        
        angularSpeed = 1.8
        waveSpeed = 9.0
        waveTime = 2.5
        waveColor = UIColor.white
        
    }
    
    func wave() -> Void {
        
        if waveShapeLayer?.path != nil {
            return
        }
        
        waveShapeLayer = CAShapeLayer()
        waveShapeLayer?.fillColor = waveColor?.cgColor
        
        self.layer .addSublayer(waveShapeLayer!)
        
        waveDisplayLink = CADisplayLink.init(target: self, selector: #selector(currentWave))
        waveDisplayLink?.add(to: RunLoop.main, forMode: RunLoopMode.commonModes)
        
        
        if waveTime > 0.0 {
            
            DispatchQueue.main.asyncAfter(deadline: .now() + waveTime, execute: { [unowned self] in
                self.stop()
            })
        }
        
    }
    
    func stop() -> Void {
        
        UIView.animate(withDuration: 1.0, animations: { [unowned self] in
            self.alpha = 0.0
        }, completion: {[unowned self] (finish) in
            self.waveDisplayLink?.invalidate()
            self.waveDisplayLink = nil
            self.waveShapeLayer?.path = nil
            self.alpha = 1.0
        })
    }
    
    func currentWave() -> Void {
        
        self.offsetX -= self.waveSpeed * (self.superview?.frame.size.width)! / UIScreen.main.bounds.width
        let width: CGFloat = self.frame.width
        let height: CGFloat = self.frame.height
        
        let path: CGMutablePath = CGMutablePath()
        path.move(to: CGPoint(x: 0, y: height / 2.0))
        
        var y: CGFloat = 0.0
        
        for var x in 0...Int(width) {
            x += 1
            y = height * sin(0.01 * (self.angularSpeed * CGFloat(x) + self.offsetX))
            path.addLine(to: CGPoint(x: CGFloat(x), y: y))
        }
        
        path.addLine(to: CGPoint(x: width, y: height))
        path.addLine(to: CGPoint(x: 0.0, y: height))
        path.closeSubpath()
        
        self.waveShapeLayer?.path = path
        
    }
}
